﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hongsh.Pay.Client.Proxy.Ssl {
    // Token: 0x0200001C RID: 28
    public class SslExtension {
        // Token: 0x1700004A RID: 74
        // (get) Token: 0x060000B3 RID: 179 RVA: 0x00002907 File Offset: 0x00000B07
        public int Value { get; }

        // Token: 0x1700004B RID: 75
        // (get) Token: 0x060000B4 RID: 180 RVA: 0x0000290F File Offset: 0x00000B0F
        public string Name { get; }

        // Token: 0x1700004C RID: 76
        // (get) Token: 0x060000B5 RID: 181 RVA: 0x00002917 File Offset: 0x00000B17
        public string Data { get; }

        // Token: 0x1700004D RID: 77
        // (get) Token: 0x060000B6 RID: 182 RVA: 0x0000291F File Offset: 0x00000B1F
        public int Position { get; }

        // Token: 0x060000B7 RID: 183 RVA: 0x00002927 File Offset: 0x00000B27
        public SslExtension(int value, string name, string data, int position) {
            //Class15.cwcZ3SWztHroi();
            //base..ctor();
            Value = value;
            Name = name;
            Data = data;
            Position = position;
        }

        // Token: 0x060000B8 RID: 184 RVA: 0x0000843C File Offset: 0x0000663C
        internal static SslExtension GetExtension(int value, byte[] data, int position) {
            string extensionName = GetExtensionName(value);
            string extensionData = GetExtensionData(value, data);
            return new SslExtension(value, extensionName, extensionData, position);
        }

        // Token: 0x060000B9 RID: 185 RVA: 0x00008464 File Offset: 0x00006664
        private static string GetExtensionData(int value, byte[] data) {
            if (value <= 5) {
                if (value == 0) {
                    StringBuilder stringBuilder = new StringBuilder();
                    int num2;
                    for (int i = 2; i < data.Length; i += 3 + num2) {
                        int num = data[i];
                        num2 = (data[i + 1] << 8) + data[i + 2];
                        string @string = Encoding.ASCII.GetString(data, i + 3, num2);
                        if (num == 0) {
                            stringBuilder.AppendFormat("{0}{1}", (stringBuilder.Length > 1) ? "; " : string.Empty, @string);
                        }
                    }
                    return stringBuilder.ToString();
                }
                if (value == 5) {
                    if (data.Length == 5 && data[0] == 1 && data[1] == 0 && data[2] == 0 && data[3] == 0 && data[4] == 0) {
                        return "OCSP - Implicit Responder";
                    }
                    return ByteArrayToString(data);
                }
            }
            else {
                switch (value) {
                    case 10:
                        return GetSupportedGroup(data);
                    case 11:
                        return GetEcPointFormats(data);
                    case 12:
                    case 14:
                    case 15:
                        break;
                    case 13:
                        return GetSignatureAlgorithms(data);
                    case 16:
                        return GetApplicationLayerProtocolNegotiation(data);
                    default:
                        if (value == 35655) {
                            return string.Format("{0} bytes", data.Length);
                        }
                        break;
                }
            }
            return ByteArrayToString(data);
        }

        // Token: 0x060000BA RID: 186 RVA: 0x000085B4 File Offset: 0x000067B4
        private static string GetSupportedGroup(byte[] data) {
            List<string> list = new List<string>();
            string result;
            if (data.Length < 2) {
                result = string.Empty;
            }
            else {
                int i = 2;
                while (i < data.Length - 1) {
                    int num = (data[i] << 8) + data[i + 1];
                    int num2 = num;
                    int num3 = num2;
                    if (num3 <= 260) {
                        switch (num3) {
                            case 1:
                                list.Add("sect163k1 [0x1]");
                                break;
                            case 2:
                                list.Add("sect163r1 [0x2]");
                                break;
                            case 3:
                                list.Add("sect163r2 [0x3]");
                                break;
                            case 4:
                                list.Add("sect193r1 [0x4]");
                                break;
                            case 5:
                                list.Add("sect193r2 [0x5]");
                                break;
                            case 6:
                                list.Add("sect233k1 [0x6]");
                                break;
                            case 7:
                                list.Add("sect233r1 [0x7]");
                                break;
                            case 8:
                                list.Add("sect239k1 [0x8]");
                                break;
                            case 9:
                                list.Add("sect283k1 [0x9]");
                                break;
                            case 10:
                                list.Add("sect283r1 [0xA]");
                                break;
                            case 11:
                                list.Add("sect409k1 [0xB]");
                                break;
                            case 12:
                                list.Add("sect409r1 [0xC]");
                                break;
                            case 13:
                                list.Add("sect571k1 [0xD]");
                                break;
                            case 14:
                                list.Add("sect571r1 [0xE]");
                                break;
                            case 15:
                                list.Add("secp160k1 [0xF]");
                                break;
                            case 16:
                                list.Add("secp160r1 [0x10]");
                                break;
                            case 17:
                                list.Add("secp160r2 [0x11]");
                                break;
                            case 18:
                                list.Add("secp192k1 [0x12]");
                                break;
                            case 19:
                                list.Add("secp192r1 [0x13]");
                                break;
                            case 20:
                                list.Add("secp224k1 [0x14]");
                                break;
                            case 21:
                                list.Add("secp224r1 [0x15]");
                                break;
                            case 22:
                                list.Add("secp256k1 [0x16]");
                                break;
                            case 23:
                                list.Add("secp256r1 [0x17]");
                                break;
                            case 24:
                                list.Add("secp384r1 [0x18]");
                                break;
                            case 25:
                                list.Add("secp521r1 [0x19]");
                                break;
                            case 26:
                                list.Add("brainpoolP256r1 [0x1A]");
                                break;
                            case 27:
                                list.Add("brainpoolP384r1 [0x1B]");
                                break;
                            case 28:
                                list.Add("brainpoolP512r1 [0x1C]");
                                break;
                            case 29:
                                list.Add("x25519 [0x1D]");
                                break;
                            case 30:
                                list.Add("x448 [0x1E]");
                                break;
                            default:
                                switch (num3) {
                                    case 256:
                                        list.Add("ffdhe2048\t[0x0100]");
                                        break;
                                    case 257:
                                        list.Add("ffdhe3072 [0x0101]");
                                        break;
                                    case 258:
                                        list.Add("ffdhe4096 [0x0102]");
                                        break;
                                    case 259:
                                        list.Add("ffdhe6144 [0x0103]");
                                        break;
                                    case 260:
                                        list.Add("ffdhe8192 [0x0104]");
                                        break;
                                    default:
                                        goto IL_314;
                                }
                                break;
                        }
                    }
                    else if (num3 != 65281) {
                        if (num3 != 65282) {
                            goto IL_314;
                        }
                        list.Add("arbitrary_explicit_char2_curves [0xFF02]");
                    }
                    else {
                        list.Add("arbitrary_explicit_prime_curves [0xFF01]");
                    }
                IL_344:
                    i += 2;
                    continue;
                IL_314:
                    list.Add(string.Format("unknown [0x{0:X4}]", num));
                    goto IL_344;
                }
                result = string.Join(", ", list.ToArray());
            }
            return result;
        }

        // Token: 0x060000BB RID: 187 RVA: 0x0000892C File Offset: 0x00006B2C
        private static string GetEcPointFormats(byte[] data) {
            List<string> list = new List<string>();
            string result;
            if (data.Length < 1) {
                result = string.Empty;
            }
            else {
                for (int i = 1; i < data.Length; i += 2) {
                    switch (data[i]) {
                        case 0:
                            list.Add("uncompressed [0x0]");
                            break;
                        case 1:
                            list.Add("ansiX962_compressed_prime [0x1]");
                            break;
                        case 2:
                            list.Add("ansiX962_compressed_char2 [0x2]");
                            break;
                        default:
                            list.Add(string.Format("unknown [0x{0:X2}]", data[i]));
                            break;
                    }
                }
                result = string.Join(", ", list.ToArray());
            }
            return result;
        }

        // Token: 0x060000BC RID: 188 RVA: 0x000089CC File Offset: 0x00006BCC
        private static string GetSignatureAlgorithms(byte[] data) {
            int num = (data[0] << 8) + data[1];
            StringBuilder stringBuilder = new StringBuilder();
            int i = 2;
            while (i < num + 2) {
                switch (data[i]) {
                    case 0:
                        stringBuilder.Append("none");
                        break;
                    case 1:
                        stringBuilder.Append("md5");
                        break;
                    case 2:
                        stringBuilder.Append("sha1");
                        break;
                    case 3:
                        stringBuilder.Append("sha224");
                        break;
                    case 4:
                        stringBuilder.Append("sha256");
                        break;
                    case 5:
                        stringBuilder.Append("sha384");
                        break;
                    case 6:
                        stringBuilder.Append("sha512");
                        break;
                    case 7:
                        goto IL_AF;
                    case 8:
                        stringBuilder.Append("Intrinsic");
                        break;
                    default:
                        goto IL_AF;
                }
            IL_D1:
                stringBuilder.AppendFormat("_", new object[0]);
                switch (data[i + 1]) {
                    case 0:
                        stringBuilder.Append("anonymous");
                        break;
                    case 1:
                        stringBuilder.Append("rsa");
                        break;
                    case 2:
                        stringBuilder.Append("dsa");
                        break;
                    case 3:
                        stringBuilder.Append("ecdsa");
                        break;
                    case 4:
                    case 5:
                    case 6:
                        goto IL_153;
                    case 7:
                        stringBuilder.Append("ed25519");
                        break;
                    case 8:
                        stringBuilder.Append("ed448");
                        break;
                    default:
                        goto IL_153;
                }
            IL_185:
                stringBuilder.AppendFormat(", ", new object[0]);
                i += 2;
                continue;
            IL_153:
                stringBuilder.AppendFormat("Unknown[0x{0:X2}]", data[i + 1]);
                goto IL_185;
            IL_AF:
                stringBuilder.AppendFormat("Unknown[0x{0:X2}]", data[i]);
                goto IL_D1;
            }
            if (stringBuilder.Length > 1) {
                stringBuilder.Length -= 2;
            }
            return stringBuilder.ToString();
        }

        // Token: 0x060000BD RID: 189 RVA: 0x00008BA0 File Offset: 0x00006DA0
        private static string GetApplicationLayerProtocolNegotiation(byte[] data) {
            List<string> list = new List<string>();
            int num;
            for (int i = 2; i < data.Length; i += 1 + num) {
                num = data[i];
                list.Add(Encoding.ASCII.GetString(data, i + 1, num));
            }
            return string.Join(", ", list.ToArray());
        }

        // Token: 0x060000BE RID: 190 RVA: 0x00008BF0 File Offset: 0x00006DF0
        private static string GetExtensionName(int value) {
            if (value <= 30032) {
                if (value <= 13172) {
                    if (value <= 2570) {
                        switch (value) {
                            case 0:
                                return "server_name";
                            case 1:
                                return "max_fragment_length";
                            case 2:
                                return "client_certificate_url";
                            case 3:
                                return "trusted_ca_keys";
                            case 4:
                                return "truncated_hmac";
                            case 5:
                                return "status_request";
                            case 6:
                                return "user_mapping";
                            case 7:
                                return "client_authz";
                            case 8:
                                return "server_authz";
                            case 9:
                                return "cert_type";
                            case 10:
                                return "supported_groups";
                            case 11:
                                return "ec_point_formats";
                            case 12:
                                return "srp";
                            case 13:
                                return "signature_algorithms";
                            case 14:
                                return "use_srtp";
                            case 15:
                                return "heartbeat";
                            case 16:
                                return "ALPN";
                            case 17:
                                return "status_request_v2";
                            case 18:
                                return "signed_certificate_timestamp";
                            case 19:
                                return "client_certificate_type";
                            case 20:
                                return "server_certificate_type";
                            case 21:
                                return "padding";
                            case 22:
                                return "encrypt_then_mac";
                            case 23:
                                return "extended_master_secret";
                            case 24:
                                return "token_binding";
                            case 25:
                                return "cached_info";
                            case 26:
                                return "quic_transports_parameters";
                            case 27:
                            case 28:
                            case 29:
                            case 30:
                            case 31:
                            case 32:
                            case 33:
                            case 34:
                            case 36:
                            case 37:
                            case 38:
                            case 39:
                                break;
                            case 35:
                                return "SessionTicket TLS";
                            case 40:
                                return "key_share";
                            case 41:
                                return "pre_shared_key";
                            case 42:
                                return "early_data";
                            case 43:
                                return "supported_versions";
                            case 44:
                                return "cookie";
                            case 45:
                                return "psk_key_exchange_modes";
                            case 46:
                                return "ticket_early_data_info";
                            case 47:
                                return "certificate_authorities";
                            case 48:
                                return "oid_filters";
                            case 49:
                                return "post_handshake_auth";
                            default:
                                if (value == 2570) {
                                    goto IL_3E6;
                                }
                                break;
                        }
                    }
                    else {
                        if (value == 6682 || value == 10794) {
                            goto IL_3E6;
                        }
                        if (value == 13172) {
                            return "next_protocol_negotiation";
                        }
                    }
                }
                else if (value <= 23130) {
                    if (value == 14906 || value == 19018 || value == 23130) {
                        goto IL_3E6;
                    }
                }
                else {
                    if (value == 27242) {
                        goto IL_3E6;
                    }
                    if (value == 30031) {
                        return "channel_id_old";
                    }
                    if (value == 30032) {
                        return "channel_id";
                    }
                }
            }
            else if (value <= 47802) {
                if (value <= 35655) {
                    if (value == 31354 || value == 35466) {
                        goto IL_3E6;
                    }
                    if (value == 35655) {
                        return "draft-agl-tls-padding";
                    }
                }
                else if (value == 39578 || value == 43690 || value == 47802) {
                    goto IL_3E6;
                }
            }
            else if (value <= 60138) {
                if (value == 51914 || value == 56026 || value == 60138) {
                    goto IL_3E6;
                }
            }
            else {
                if (value == 64250) {
                    goto IL_3E6;
                }
                if (value == 65281) {
                    return "renegotiation_info";
                }
                if (value == 65282) {
                    return "Draft version of TLS 1.3";
                }
            }
            return string.Format("unknown_{0:x2}", value);
        IL_3E6:
            return "Reserved (GREASE)";
        }

        // Token: 0x060000BF RID: 191 RVA: 0x00008FEC File Offset: 0x000071EC
        private static string ByteArrayToString(byte[] data) {
            return string.Join(" ", from x in data
                                    select x.ToString("X2"));
        }
    }
}
